package com.yylc.cscore.bean.query;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.yylc.cscore.util.R;
import lombok.Data;

import java.util.Date;
import java.util.List;

/**
 * @author xiangnan
 * date 2018/5/11
 */
@Data
public class ServiceTaskQueryBean {
    private String serviceId;
    private String userType;        // YYLX YYX

    private String userId;
    private String userPhone;
    private String taskType;        // 问题类型
    private String taskLabel;       // 任务标签
    private String taskLabelSort;   // 标签排序
    private int operateStatus;
    private Date userReqTimeStart; // 用户问题查询起止时间
    private Date userReqTimeEnd;
    private Date operateTimeStart; // 客服跟踪查询起止时间
    private Date operateTimeEnd;
    private String userLabel;
    private String registerFromName;           // 注册渠道
    private String registerDeviceFromName;     // 下载渠道
    private int current;
    private int size;
    private int start;

    @Data
    public static class Raw {
        private String serviceId;
        private String userId;
        private String userPhone;
        private String taskType;         // 问题类型
        private String taskLabel;
        private String taskLabelSort;
        private int operateStatus;
        private String userType;         // YYLX YYX
        private String userReqTimeStart; // 用户问题查询起止时间
        private String userReqTimeEnd;
        private String operateTimeStart; // 客服跟踪查询起止时间
        private String operateTimeEnd;
        private String userLabel;
        private String registerFromName;
        private String registerDeviceFromName;
        private int current;
        private int size;

        public ServiceTaskQueryBean convert() {
            ServiceTaskQueryBean bean = new ServiceTaskQueryBean();

            bean.setServiceId(StrUtil.isBlank(serviceId) ? null : serviceId);
            bean.setUserId(StrUtil.isBlank(userId) ? null : userId);
            bean.setUserPhone(StrUtil.isBlank(userPhone) ? null : userPhone);
            bean.setTaskType(StrUtil.isBlank(taskType) ? null : taskType);
            bean.setTaskLabel(StrUtil.isBlank(taskLabel) ? "" : taskLabel);
            bean.setTaskLabelSort(StrUtil.isBlank(taskLabelSort) ? "" : taskLabelSort);
            bean.setOperateStatus(operateStatus);
            bean.setUserLabel(userLabel);
            bean.setRegisterFromName(StrUtil.isBlank(registerFromName) ? null : registerFromName);
            bean.setRegisterDeviceFromName(StrUtil.isBlank(registerDeviceFromName) ? null : registerDeviceFromName);

            if (!R.userType.YYLC.equals(userType) && !R.userType.YYX.equals(userType)) {
                throw new RuntimeException("参数错误");
            }
            bean.setUserType(userType);

            List<Date> dateList = convert2Date(userReqTimeStart, userReqTimeEnd);
            bean.setUserReqTimeStart(dateList.get(0));
            bean.setUserReqTimeEnd(dateList.get(1));

            dateList = convert2Date(operateTimeStart, operateTimeEnd);
            bean.setOperateTimeStart(dateList.get(0));
            bean.setOperateTimeEnd(dateList.get(1));

            if (current <= 0 || size <= 0) {
                throw new RuntimeException("参数错误");
            }
            bean.setCurrent(current);
            bean.setSize(size);

            return bean;
        }

        /**
         * 对前端传进来的日期进行转换，传进来的日期格式为 Date().getTime()
         */
        private List<Date> convert2Date(String start, String end) {
            if (start.length() != end.length()) {
                throw new RuntimeException("参数错误");
            }

            return CollectionUtil.newArrayList(convert2Date(start), convert2Date(end));
        }
        private Date convert2Date(String time) {
            if (StrUtil.isBlank(time)) {
                return null;
            }


            return new Date(Long.parseLong(time));
        }

    }
}
